// pages/my/cooper/cooper.js
var app = getApp(), api = require("../../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    winWidth: '',
    winHeight: '',
    province: '',
    city: '',
    area: '',
    show: false,
    imgDefault:'+',
    imgUrl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    app.request({
      url:api.setting,
      method:'post',
      success:function(res){
       _this.setData({
         img: res.data.partner_img
       })
      }
    })
    let res = wx.getSystemInfoSync()
    this.setData({
      winWidth: res.windowWidth,
      winHeight: res.windowHeight + 120
    })
  },
  formSubmit:function(res){
    var _this = this,formData=res.detail.value;
    if (_this.data.currentTab==0){
      app.request({
        url: api.apply,
        data:{
          companyname: formData.companyname,
          province: _this.data.province,
          city: _this.data.city,
          area: _this.data.area,
          imgUrl:_this.data.imgUrl,
          mobile:formData.mobile,
          email: formData.email,
          username:formData.username,
          type:1
        },
        method:'post',
        success:function(res){
            if(res.code===0){
                wx.showToast({
                  title: res.msg,
                  success:function(){
                    setTimeout(function(){
                      wx.navigateBack({

                      })
                    },1000)
                  }
                })      
            }
        }
      })
    }else{

      app.request({
        url: api.apply,
        data: {
          mobile: formData.mobile,
          email: formData.email,
          username: formData.username,
          sex:formData.sex,
          age:formData.age,
          weixin:formData.weixin,
          type: 2
        },
        method: 'post',
        success: function (res) {
          if (res.code === 0) {
            wx.showToast({
              title: res.msg,
              success: function () {
                setTimeout(function () {
                  wx.navigateBack({

                  })
                }, 1000)
              }
            })
          }
        }
      })
    }
  },
  swiperTab: function (e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  cilckTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // 选择区域
  chooseAddress: function () {
    var that = this;
    that.setData({
      show: true
    })
  },
  sureSelectAreaListener: function (e) {
    var that = this;
    that.setData({
      show: false,
      province: e.detail.currentTarget.dataset.province,
      city: e.detail.currentTarget.dataset.city,
      area: e.detail.currentTarget.dataset.area
    })
  },
  chooseImg:function(){
    var that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        var tempFilePath=res.tempFilePaths[0]
        wx.uploadFile({
          url: api.upload,
          filePath: tempFilePath,
          name: 'file',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success:function(res){
            var res = JSON.parse(res.data);
            console.log(res);
            if(res.code!==0){
              wx.showToast({
                title: '图片上传失败',
                icon:'none',
              })
              return false;
            }
            that.setData({
              imgUrl:res.data.src,
              imgDefault:""
            })
          }
        })
     
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})